{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IBM Watsonx AI Text Generation and Chat with AgentOps\n",
    "\n",
    "This notebook demonstrates how to use IBM Watsonx AI for basic text generation and chat completion tasks with AgentOps instrumentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and initialize AgentOps:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "outputs": [],
   "source": "import agentops\nfrom ibm_watsonx_ai import Credentials\nfrom ibm_watsonx_ai.foundation_models import ModelInference\nfrom dotenv import load_dotenv\nimport os\n\n# Load environment variables\nload_dotenv()\nos.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")\n\n# Initialize AgentOps\nagentops.init(tags=[\"watsonx-text-chat\", \"agentops-example\"])"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize IBM Watsonx AI Credentials\n",
    "\n",
    "To use IBM Watsonx AI, you need to set up your credentials and project ID."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "outputs": [],
   "source": "# Initialize credentials - replace with your own API key\n# Best practice: Store API keys in environment variables\n# Ensure WATSONX_API_KEY is set in your .env file or environment\nos.environ[\"WATSONX_API_KEY\"] = os.getenv(\"WATSONX_API_KEY\", \"your_watsonx_api_key_here\")\n\ncredentials = Credentials(\n    url=os.getenv(\"WATSONX_URL\", \"https://eu-de.ml.cloud.ibm.com\"),\n    api_key=os.environ[\"WATSONX_API_KEY\"],\n)\n\n# Project ID for your IBM Watsonx project\nproject_id = os.getenv(\"WATSONX_PROJECT_ID\", \"your-project-id-here\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text Generation\n",
    "\n",
    "Let's use IBM Watsonx AI to generate text based on a prompt:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize text generation model\n",
    "gen_model = ModelInference(model_id=\"google/flan-ul2\", credentials=credentials, project_id=project_id)\n",
    "\n",
    "# Generate text with a prompt\n",
    "prompt = \"Write a short poem about artificial intelligence:\"\n",
    "response = gen_model.generate_text(prompt)\n",
    "print(f\"Generated Text:\\n{response}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat Completion\n",
    "\n",
    "Now, let's use a different model for chat completion:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize chat model\n",
    "chat_model = ModelInference(\n",
    "    model_id=\"meta-llama/llama-3-3-70b-instruct\", credentials=credentials, project_id=project_id\n",
    ")\n",
    "\n",
    "# Format messages for chat\n",
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are a helpful AI assistant.\"},\n",
    "    {\"role\": \"user\", \"content\": \"What are the three laws of robotics?\"},\n",
    "]\n",
    "\n",
    "# Get chat response\n",
    "chat_response = chat_model.chat(messages)\n",
    "print(f\"Chat Response:\\n{chat_response['choices'][0]['message']['content']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Another Chat Example\n",
    "\n",
    "Let's try a different type of query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# New chat messages\n",
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are an expert in machine learning.\"},\n",
    "    {\"role\": \"user\", \"content\": \"Explain the difference between supervised and unsupervised learning in simple terms.\"},\n",
    "]\n",
    "\n",
    "# Get chat response\n",
    "chat_response = chat_model.chat(messages)\n",
    "print(f\"Chat Response:\\n{chat_response['choices'][0]['message']['content']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clean Up\n",
    "\n",
    "Finally, let's close the persistent connection with the models:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Close connections\n",
    "gen_model.close_persistent_connection()\n",
    "chat_model.close_persistent_connection()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}